home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-30 | 24.0 KB | 1,125 lines |
- /*
- File: CDialogs.cp
-
- Contains: Layer built on top of the Dialog Manager
-
- Written by: Arno Gourdol
-
- Copyright: © 1994-1996 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #include "CDialogs.h"
- #include "assert.h"
-
- #include <LowMem.h> // for LMGetGrayRgn()
- #include <Memory.h>
- #include <Script.h>
-
- #include "CFonts.h"
- #include "HTMLParser.h"
-
-
- CDialog *CDialog::gDialogList = NULL;
-
-
-
- class LazyHandle
- {
- public:
- inline LazyHandle(void);
- inline ~LazyHandle();
-
- inline void SetSize(Size size);
- inline Size GetSize(void);
- inline operator Handle(void) { return fHandle;};
- inline Handle Set(void* pointer, Size size);
- private:
- Handle fHandle;
- };
-
-
-
- inline LazyHandle::LazyHandle(void) :
- fHandle(NULL)
- {
- }
-
-
-
- inline LazyHandle::~LazyHandle()
- {
- if (fHandle != NULL)
- {
- DisposeHandle(fHandle);
- }
- }
-
-
-
- inline void LazyHandle::SetSize(Size size)
- {
- if (fHandle == NULL)
- {
- fHandle = NewHandle(size);
- }
- else
- {
- SetHandleSize(fHandle, size);
- if (MemError() != noErr)
- {
- if (fHandle != NULL)
- DisposeHandle(fHandle);
- fHandle = NULL;
- }
- }
- }
-
- inline Size LazyHandle::GetSize(void)
- {
- if (fHandle == NULL)
- {
- return 0;
- }
- else
- {
- return GetHandleSize(fHandle);
- }
- }
-
-
-
- inline Handle LazyHandle::Set(void* pointer, Size size)
- {
- SetSize(size);
- if (fHandle != NULL)
- ::BlockMoveData(pointer, *fHandle, size);
- return fHandle;
- }
-
-
-
- // --------------------------------------------------------------------
- // CDialog
- // --------------------------------------------------------------------
-
- CDialog::CDialog(SInt16 dialogID, FontCode fontCode) :
- fDialogID(dialogID),
- fDialog(NULL),
- fDefaultButton(-1),
- fCancelButton(-1),
- fNextDialog(NULL),
- fNumShortcuts(0),
- fShortcuts(NULL)
- {
- for (int i = 0; i < kDialogParametersCount; i++)
- {
- fParameters[i].key[0] = 0;
- fParameters[i].value[0] = 0;
- fParameters[i].dialogItem = 0;
- }
-
- for (i = 0; i < kStyledTextCount; i++)
- {
- fStyleTextItem[i] = 0;
- fStyledText[i] = NULL;
- }
-
- fUserItemCallback = NewUserItemProc(CDialog::UserItemCallback);
-
- // Add it to the list of dialogs
- SetNextDialog(gDialogList);
- gDialogList = this;
-
- fDialogFont.SetFont(smSystemScript, fontCode);
- }
-
-
-
- // --------------------------------------------------------------------
- // ~CDialog
- // --------------------------------------------------------------------
-
- CDialog::~CDialog(void)
- {
- for (int i = 0; i < kStyledTextCount; i++)
- {
- if (fStyleTextItem[i] != 0 && fStyledText[i] != NULL)
- {
- TEDispose(fStyledText[i]);
- }
- }
-
- if (fUserItemCallback != NULL)
- DisposeRoutineDescriptor(fUserItemCallback);
-
- // remove this from the list of dialogs
- if (gDialogList == this)
- {
- gDialogList = GetNextDialog();
- }
- else if (gDialogList != NULL)
- {
- for (CDialog *theDialog = gDialogList; theDialog != NULL;
- theDialog = theDialog->GetNextDialog())
- {
- if (theDialog->GetNextDialog() == this)
- {
- theDialog->SetNextDialog(this->GetNextDialog());
- break;
- }
- }
- }
-
- if (fShortcuts != NULL)
- DisposePtr((Ptr)fShortcuts);
- }
-
-
-
- // --------------------------------------------------------------------
- // Close
- // --------------------------------------------------------------------
-
- void CDialog::Close(void)
- {
- if (GetDialogRef() != NULL)
- {
- // Get all the parameters
- for (int i = 0; i < kDialogParametersCount; i++)
- {
- if (fParameters[i].dialogItem > 0)
- {
- GetItemText(fParameters[i].dialogItem, fParameters[i].value);
- }
- }
-
-
- ::DisposeDialog(GetDialogRef());
- fDialog = NULL;
- // In fact, DisposeDialog also dispose of the window,
- // therefore...
- fWindow = NULL;
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // GetCDialog
- // --------------------------------------------------------------------
-
- CDialog* CDialog::GetCDialog(DialogRef dialog)
- {
- CDialog *theDialog;
- for (theDialog = gDialogList; theDialog != NULL;
- theDialog = theDialog->GetNextDialog())
- {
- if (theDialog->GetDialogRef() == dialog)
- break;
- }
- return theDialog;
- }
-
-
-
- // --------------------------------------------------------------------
- // Draw
- // --------------------------------------------------------------------
-
- void CDialog::Draw(TDrawContext& drawContext)
- {
- #pragma unused(drawContext)
-
- }
-
-
-
- // --------------------------------------------------------------------
- // SetItemRect
- // --------------------------------------------------------------------
-
- void CDialog::SetItemRect(short item, const CRect& newRect)
- {
- CRect r;
- Handle h;
- short type;
-
- ::GetDialogItem(GetDialogRef(), item, &type, &h, r);
- if (newRect != r)
- {
- if ((type & ctrlItem) == ctrlItem)
- {
- ::MoveControl((ControlRef)h, newRect.Left(), newRect.Top());
- }
- else if ((type & editText) == editText &&
- (((DialogPeek)GetDialogRef())->editField + 1) == item)
- {
- TEHandle te = ((DialogPeek)GetDialogRef())->textH;
- ::TEDeactivate(te);
- (**te).viewRect = (**te).destRect = newRect;
- ::TECalText(te);
- ::TEActivate(te);
- }
- ::SetDialogItem(GetDialogRef(), item, type, h, newRect);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // SetItemControl
- // --------------------------------------------------------------------
-
- void CDialog::SetItemControl(short item, ControlRef control)
- {
- Handle h = GetItemData(item);
- if (h != NULL)
- ::DisposeControl((ControlRef)h);
- SetItemData(item, (Handle)control);
- }
-
-
-
- // --------------------------------------------------------------------
- // SetItemText
- // --------------------------------------------------------------------
-
- void CDialog::SetItemText(short item, ConstStr255Param s)
- {
- assert(GetItemType(item) == statText ||
- GetItemType(item) == editText);
-
- Handle h = GetItemData(item);
- Str255 s2;
- GetDialogItemText(h, s2);
-
- // Set the text only if different, to avoid flickering
- if (!EqualString(s, s2, true, true))
- SetDialogItemText(h, s);
- }
-
-
-
- // --------------------------------------------------------------------
- // PasteText
- // --------------------------------------------------------------------
-
- void CDialog::PasteText(Ptr text, short size)
- {
- TEHandle te = ((DialogPeek)GetDialogRef())->textH;
- if ((**te).selEnd - (**te).selStart > 0)
- TEDelete(te);
- TEInsert(text, size, te);
- }
-
-
-
- // --------------------------------------------------------------------
- // ItemHit
- // --------------------------------------------------------------------
-
- void CDialog::ItemHit(short itemHit)
- {
- if (itemHit == fDefaultButton)
- {
- if (CloseRequested())
- Close();
- }
- else if (itemHit == fCancelButton)
- {
- Close();
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // MakeWindow
- // --------------------------------------------------------------------
-
- WindowRef CDialog::MakeWindow(void)
- {
- fDialog = MakeDialog();
-
- DoPrepareDialog();
-
- return ::GetDialogWindow(fDialog);
- }
-
-
-
- // --------------------------------------------------------------------
- // MakeDialog
- // --------------------------------------------------------------------
-
- DialogRef CDialog::MakeDialog(void)
- {
- assert(GetDialogRef() == NULL);
- DialogRef newDialog;
-
- // We want color dialogs. However, the caller does not necessarily
- // provide a 'dctb' and there isn't a GetNewCDialog routine.
- // This requires allocating the dialog with NewCDialog.
-
- DialogTemplate **dlogTemplate =
- (DialogTemplate **)GetResource('DLOG', fDialogID); // (-)
- assert(dlogTemplate != NULL);
-
- if (GetResource('dctb', fDialogID) != NULL)
- {
- // CDialogs never come up until you show them explicitly!
- (*dlogTemplate)->visible = false;
- newDialog = GetNewDialog(fDialogID, NULL, (WindowRef)-1); // (-)
- assert(newDialog != NULL);
- }
- else
- {
- ::HLock((Handle)dlogTemplate);
-
- Handle itemList = ::GetResource('DITL', (**dlogTemplate).itemsID);
- assert(itemList != NULL);
- ::DetachResource(itemList);
-
- newDialog = ::NewColorDialog(NULL, &(**dlogTemplate).boundsRect,
- (**dlogTemplate).title, false,
- (**dlogTemplate).procID,
- (WindowRef)-1, (**dlogTemplate).goAwayFlag,
- (**dlogTemplate).refCon, itemList); // (-)
- assert(newDialog != NULL);
-
- }
-
- ::ReleaseResource((Handle)dlogTemplate); // (-)
-
- // Use the dialog font in the dialog's grafport
- fDialogFont.SetPortFont(GetGrafPtr());
-
- return newDialog;
- }
-
-
-
- // --------------------------------------------------------------------
- // CheckKeyboardShortcuts
- // --------------------------------------------------------------------
-
- Boolean CDialog::CheckKeyboardShortcuts(const EventRecord &event)
- {
- Boolean result = false;
- short itemHit = 0;
-
- if (GetDialogRef() != NULL)
- {
- if (event.what == keyDown)
- {
- switch (event.message & charCodeMask)
- {
- case 0x0d:
- case 0x03:
- itemHit = fDefaultButton;
- break;
-
- case 0x1b:
- itemHit = fCancelButton;
- break;
-
- case 0x2E:
- if (event.modifiers & cmdKey)
- itemHit = fCancelButton;
- break;
- default:
- break;
- }
- }
-
- if (fShortcuts != NULL && itemHit == 0 &&
- event.what == keyDown && (event.modifiers & cmdKey) != 0)
- {
- char c = (event.message & 0xff);
- for (int i = 0; i < fNumShortcuts && !result; i++)
- {
- if (fShortcuts[i].key == c)
- {
- itemHit = fShortcuts[i].item;
- result = true;
- }
- }
- }
-
- // Items that are controls want some extra handling.
- // Too bad we can't handle radio buttons!
- if (itemHit >= 1 && itemHit <= CountDITL(GetDialogRef()))
- {
- if ((GetItemType(itemHit) & ctrlItem) == ctrlItem)
- {
- if (IsEnabled(itemHit))
- {
- switch (GetItemType(itemHit) & ~ctrlItem)
- {
- case btnCtrl:
- FlashButton(itemHit);
- break;
- case chkCtrl:
- SetItemValue(itemHit, !GetItemValue(itemHit));
- break;
- }
- }
- else
- {
- // The item was disabled
- itemHit = 0;
- result = false;
- }
- }
- if (itemHit > 0)
- ItemHit(itemHit);
- }
- }
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // GetStyledText
- // --------------------------------------------------------------------
-
- TEHandle CDialog::GetStyledText(short item)
- {
- TEHandle result = NULL;
-
- for (int i = 0; i < kStyledTextCount; i++)
- {
- if (fStyleTextItem[i] == item)
- {
- result = fStyledText[i];
- break;
- }
- }
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // SetStyledText
- // --------------------------------------------------------------------
-
- void CDialog::SetStyledText(short item, TEHandle styledText)
- {
- for (int i = 0; i < kStyledTextCount; i++)
- {
- if (fStyleTextItem[i] == item)
- {
- if (fStyledText[i] != NULL)
- {
- TEDispose(fStyledText[i]);
- }
- fStyledText[i] = styledText;
- break;
- }
- }
-
- if (i == kStyledTextCount)
- {
- // No existing items were found
- for (i = 0; i < kStyledTextCount; i++)
- {
- if (fStyleTextItem[i] <= 0)
- {
- fStyleTextItem[i] = item;
- fStyledText[i] = styledText;
- break;
- }
- }
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // FilterEvent
- // --------------------------------------------------------------------
-
- Boolean CDialog::FilterEvent(const EventRecord& event)
- {
- Boolean result = false;
-
- if (CWindow::FilterEvent(event))
- {
- result = true;
- }
- else if (CheckKeyboardShortcuts(event))
- {
- // It was a keyboard shortcut
- result = true;
- }
- else if (event.what == keyDown && (event.modifiers & cmdKey) != 0)
- {
- // This is an unknown keyboard shortcuts, pass it up
- result = false;
- }
- else if (IsDialogEvent(&event))
- {
- short itemHit;
-
- // call the standard event filter
- {
- EventRecord eventCopy = event;
- result = StdFilterProc(GetDialogRef(), &eventCopy, &itemHit);
- }
-
- if (!result)
- {
- CFontSpec saveFontSpec;
- saveFontSpec.Save();
-
- {
- fDialogFont.Use();
- {
- DialogRef dialog;
- if (event.what == mouseDown)
- {
- ControlRef control = GetItemControl(1);
- }
- if (DialogSelect(&event, &dialog, &itemHit))
- result = true;
- }
- }
- saveFontSpec.Restore();
- }
-
- if (result)
- ItemHit(itemHit);
- }
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // DoPrepareDialog
- // --------------------------------------------------------------------
-
- void CDialog::DoPrepareDialog(void)
- {
- TDrawContext drawContext((GrafPtr)GetWindowPort(
- GetDialogWindow(fDialog)));
- if (drawContext.Lock())
- {
- // Set the clip region to empty to avoid blinking
- RgnHandle saveRegion;
- saveRegion = NewRgn();
- GetClip(saveRegion);
-
- ClipRect(CRect(0, 0, 0, 0));
-
- PrepareDialog();
-
- SetClip(saveRegion);
- DisposeRgn(saveRegion);
-
- drawContext.Unlock();
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // PrepareDialog
- // --------------------------------------------------------------------
-
- void CDialog::PrepareDialog(void)
- {
- LazyHandle itemText;
- SInt16 focusedEditField = 0;
-
- // Replace parameters in the dialog
- SubstituteParameters();
-
- for (int i = 0; i < kStyledTextCount; i++)
- {
- if (fStyleTextItem[i] != 0 && fStyledText[i] != NULL)
- ::TEDispose(fStyledText[i]);
- fStyleTextItem[i] = 0;
- fStyledText[i] = NULL;
- }
-
- // Use the dialog font
- fDialogFont.Use();
-
- // Loop through all dialog items
- for (int item = 1, itemCount = CountDITL(GetDialogRef());
- item <= itemCount; item++)
- {
- short itemType = GetItemType(item);
-
- // Set the font if it is the first text field
- if (itemType == kEditTextDialogItem && focusedEditField == 0)
- {
- TEHandle te = ((DialogPeek)GetDialogRef())->textH;
- (**te).txSize = fDialogFont.GetSize();
- (**te).txFont = fDialogFont.GetFont();
- ::TECalText(te);
-
- // Set the selection to the first edit field
- focusedEditField = item;
- SelectItemText(focusedEditField);
- }
-
- if (itemType == kStaticTextDialogItem)
- {
- TEHandle textEdit;
- Str255 itemTextString;
- GetItemText(item, itemTextString);
- itemText.Set(&itemTextString[1], itemTextString[0]);
- ConvertHTMLToStyledText(itemText, GetItemRect(item), &textEdit);
- if (textEdit != NULL)
- {
- SetStyledText(item, textEdit);
- SetItemType(item, userItem);
- ::InvalRect(GetItemRect(item));
- itemType = userItem;
- }
- }
-
- // Re-route user items to our callback
- if (itemType == userItem)
- SetDrawingProc(item, fUserItemCallback);
- }
-
- if (fDefaultButton > 0)
- SetDefaultButton(fDefaultButton);
- if (fCancelButton > 0)
- SetCancelButton(fCancelButton);
- ::SetDialogTracksCursor(GetDialogRef(), true);
- }
-
-
-
- // --------------------------------------------------------------------
- // SubstituteParameters
- // --------------------------------------------------------------------
-
- void CDialog::SubstituteParameters(void)
- {
- // This routine will substitute parameters in the dialog, by
- // replacing any instance of a key, for example <name>, appearing
- // in a static or editable text dialog item, by a corresponding
- // value.
- // In addition, if an editable text item only contains a key,
- // the value entered in the dialog will be associated with the key.
- // This is an extended version of the "^0" parameter substitution
- // done by the Dialog Manager.
-
- LazyHandle substitutionText;
- LazyHandle itemText;
-
- // Reset the association between parameters and dialog items
- for (int i = 0; i < kDialogParametersCount; i++)
- fParameters[i].dialogItem = 0;
-
- // Loop through all dialog items
- for (int item = 1, itemCount = ::CountDITL(GetDialogRef());
- item <= itemCount; item++)
- {
- SInt16 itemType = GetItemType(item);
-
- // If a static or editable text item...
- if (itemType == kEditTextDialogItem ||
- itemType == kStaticTextDialogItem)
- {
- Boolean itemTextChanged = false;
-
- // Copy the text to a handle
- Str255 itemTextString;
- GetItemText(item, itemTextString);
- itemText.Set(&itemTextString[1], itemTextString[0]);
-
- // Loop through the parameters
- for (int j = 0; j < kDialogParametersCount; j++)
- {
- // If the parameter is used, i.e., as a non-empty key
- if (fParameters[j].key[0] != 0)
- {
- if (itemType == kEditTextDialogItem &&
- ::IdenticalString(itemTextString, fParameters[j].key,
- NULL) == 0)
- {
- // If the edit field only contains the key,
- // associate the item index with the parameter.
- // The value of the parameter will be updated
- // when the text changes
- fParameters[j].dialogItem = item;
- }
- {
- // Replace the key by the value of the parameter
- // using the Script Manager
- substitutionText.Set(&fParameters[j].value[1],
- fParameters[j].value[0]);
- if (::ReplaceText(itemText, substitutionText,
- fParameters[j].key) > 0)
- itemTextChanged = true;
- }
- }
- }
-
- if (itemTextChanged)
- {
- // The text of the item has changed, put the
- // modified text back in the dialog item
- Str255 s;
-
- s[0] = itemText.GetSize();
- ::BlockMoveData(*itemText, &s[1], s[0]);
-
- SetItemText(item, s);
-
- // Invalidate the rectangle of the item so
- // it gets updated.
- // Although SetItemText draws the new text immediately,
- // the clip region may be set to NULL by the caller to
- // avoid flashing
- ::InvalRect(GetItemRect(item));
- }
- }
-
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // DrawUserItem
- // --------------------------------------------------------------------
-
- void CDialog::DrawUserItem(TDrawContext& graphics, short item,
- const CRect& frame)
- {
- #pragma unused(graphics)
-
- // If there is a styled text record associated with this item,
- // draw it now
- TEHandle styledText = GetStyledText(item);
-
- if (styledText != NULL)
- {
- TEUpdate(frame, styledText);
- }
-
- }
-
-
-
- // --------------------------------------------------------------------
- // AddKeyboardShortcuts
- // --------------------------------------------------------------------
-
- void CDialog::AddKeyboardShortcuts(short stringID)
- {
- assert(fNumShortcuts == 0);
- assert(fShortcuts == NULL);
- short **numShortcuts = (short **)GetResource('STR#', stringID);
- if (numShortcuts != NULL)
- {
- assert(**numShortcuts >= 0);
- fShortcuts = (Shortcut*)NewPtr(sizeof(Shortcut) *
- (**numShortcuts));
- if (fShortcuts != NULL)
- {
- for (int i = 1; i <= **numShortcuts; i++)
- {
- Str255 s;
- ::GetIndString(s, stringID, i);
- assert(s[0] > 2);
- assert(s[2] == ':');
- fShortcuts[fNumShortcuts].key = s[1];
- s[2] = s[0] - 2;
- long n;
- ::StringToNum(s + 2, &n);
- fShortcuts[fNumShortcuts].item = n;
- fNumShortcuts++;
- }
- }
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // FirstTextField
- // --------------------------------------------------------------------
-
- short CDialog::FirstTextField(void)
- {
- short result = 0;
-
- short curItem;
-
- short itemCount = ::CountDITL(GetDialogRef());
-
- // Loop thru the items in the dialog after curItem
- for (curItem = 1; curItem <= itemCount; curItem++)
- {
- if (GetItemType(curItem) == editText)
- {
- result = curItem;
- break;
- }
- }
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // LastTextField
- // --------------------------------------------------------------------
-
- short CDialog::LastTextField(void)
- {
- short result = 0;
-
- short curItem = ::CountDITL(GetDialogRef());
-
- // Loop thru the items in the dialog before curItem
- for (; curItem > 0; curItem--)
- {
- if (GetItemType(curItem) == editText)
- {
- result = curItem;
- break;
- }
- }
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // FirstTextField
- // --------------------------------------------------------------------
-
- short CDialog::PreviousTextField(short item)
- {
- short result = 0;
-
- short curItem;
-
- if (item == 0)
- {
- // if item was 0, start from the item after the current one.
- // Add 1 because editField is in range [0..n]
- curItem = ((DialogPeek)GetDialogRef())->editField + 1;
- }
-
- // Loop thru the items in the dialog before curItem
- for (curItem = curItem - 1; curItem > 0; curItem--)
- {
- if (GetItemType(curItem) == editText)
- {
- result = curItem;
- break;
- }
- }
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // NextTextField
- // --------------------------------------------------------------------
- // Returns the ID of the next text field in the dialog,
- // or 0 if no more text fields
-
- short CDialog::NextTextField(short item)
- {
- short result = 0;
-
- short curItem;
-
- if (item == 0)
- {
- // if item was 0, start from the item after the current one.
- // Add 1 because editField is in range [0..n]
- curItem = ((DialogPeek)GetDialogRef())->editField + 1;
- }
-
- short itemCount = ::CountDITL(GetDialogRef());
-
- // Loop thru the items in the dialog after curItem
- for (curItem = curItem + 1; curItem <= itemCount; curItem++)
- {
- if (GetItemType(curItem) == editText)
- {
- result = curItem;
- break;
- }
- }
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // SetParameter
- // --------------------------------------------------------------------
-
- void CDialog::SetParameter(ConstStr15Param key, ConstStr255Param value)
- {
- assert(key[0] <= 15);
- assert(key[0] > 0);
-
- for (SInt16 i = 0; i < kDialogParametersCount; i++)
- {
- if (IdenticalString(key, fParameters[i].key, NULL) == 0 ||
- fParameters[i].key[0] == 0)
- {
- ::BlockMoveData(key, fParameters[i].key, key[0] + 1);
- ::BlockMoveData(value, fParameters[i].value, value[0] + 1);
- break;
- }
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // GetParameter
- // --------------------------------------------------------------------
-
- void CDialog::GetParameter(ConstStr15Param key, Str255 value)
- {
- assert(key[0] <= 15);
- assert(key[0] > 0);
-
- for (SInt16 i = 0; i < kDialogParametersCount; i++)
- {
- if (IdenticalString(key, fParameters[i].key, NULL) == 0)
- {
- if (fParameters[i].dialogItem > 0)
- {
- GetItemText(fParameters[i].dialogItem, fParameters[i].value);
- }
- ::BlockMoveData(fParameters[i].value, value,
- fParameters[i].value[0] + 1);
- break;
- }
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // SetCancelButton
- // --------------------------------------------------------------------
-
- void CDialog::SetCancelButton(short itemNumber)
- {
- assert(itemNumber > 0);
-
- fCancelButton = itemNumber;
- ::SetDialogCancelItem(GetDialogRef(), itemNumber);
- }
-
-
-
- // --------------------------------------------------------------------
- // SetDefaultButton
- // --------------------------------------------------------------------
-
- void CDialog::SetDefaultButton(short itemNumber)
- {
- assert(itemNumber > 0);
-
- short oldDefault = fDefaultButton;
-
- // set the new default button
- fDefaultButton = itemNumber;
- ::SetDialogDefaultItem(GetDialogRef(), itemNumber);
-
- // Update the old button
- if (oldDefault > 0)
- {
- CRect r = GetItemRect(oldDefault);
- r.InsetBy(-4);
- EraseRect(r);
- if (IsWindowHilited(GetWindowRef()))
- {
- UpdateDialog(GetDialogRef(), CTempRgn(r));
- }
- }
-
- // Update the new button
- if (itemNumber > 0 && oldDefault != itemNumber)
- {
- CRect r = GetItemRect(oldDefault);
- r.InsetBy(-4);
- EraseRect(r);
- if (IsWindowHilited(GetWindowRef()))
- {
- UpdateDialog(GetDialogRef(), CTempRgn(r));
- }
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // SetDefaultButtonName
- // --------------------------------------------------------------------
-
- void CDialog::SetDefaultButtonName(ConstStr63Param buttonName)
- {
- assert(GetDialogRef() != NULL);
- assert(fDefaultButton > 0);
- SetItemTitle(fDefaultButton, buttonName);
- }
-
-
-
- // --------------------------------------------------------------------
- // UserItemCallback
- // --------------------------------------------------------------------
-
- pascal void CDialog::UserItemCallback(DialogRef dialog, short item)
- {
- CFontSpec saveFont;
- saveFont.Save();
- {
- // ??? Not sure that this clipping is quite necessary here
- CClip saveClip;
- saveClip.Save();
- {
- CDialog* dialogObject = CDialog::GetCDialog(dialog);
- assert(dialogObject != NULL);
- dialogObject->fDialogFont.Use();
- {
- TDrawContextIterator iterator(dialogObject->GetGrafPtr(),
- dialogObject->GetItemRect(item));
-
- while (iterator != iterator.end())
- {
- if ((*iterator).Lock())
- {
- dialogObject->DrawUserItem(*iterator, item,
- dialogObject->GetItemRect(item));
- (*iterator).Unlock();
- }
- ++iterator;
- }
- }
- }
- saveClip.Restore();
- }
- saveFont.Restore();
- }
-